package com.lungle.warmdiary.app.service.push;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.lungle.warmdiary.app.AppConstant;
import com.lungle.warmdiary.app.model.SimpleBackPage;
import com.lungle.warmdiary.app.utils.TDevice;
import com.lungle.warmdiary.app.view.MainActivity;
import com.lungle.warmdiary.app.view.common.SimpleBackActivity;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by God on 2016/8/31.
 */
public class SchemeReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            System.out.println("收到了自定义消息。消息内容是：" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            // 自定义消息不会展示在通知栏，完全要开发者写代码去处理
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            System.out.println("收到了通知");
            // 在这里可以做些统计，或者做些其他工作
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            if (TDevice.isRunning(context, "com.lungle.warmdiary")) {
                Intent mainIntent = new Intent(context, MainActivity.class);
                mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                Intent messageIntent = new Intent(context, SimpleBackActivity.class);
                messageIntent.putExtra(SimpleBackActivity.BUNDLE_KEY_PAGE, SimpleBackPage.MESSAGE.getValue());

                Intent[] intents = {mainIntent, messageIntent};
                context.startActivities(intents);

            } else {
                Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage("com.socialtouch.meida");
                launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                Bundle args = new Bundle();
                launchIntent.putExtra(AppConstant.EXTRA_BUNDLE, args);
                context.startActivity(launchIntent);
            }
        } else {
        }
    }
}
